package xyz.lisys.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import xyz.lisys.model.CedUsers;
import xyz.lisys.service.CedUsersService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport {
    private Integer id;
    private CedUsers currentUser;
//    @Resource
//    private CedUsersService userService;

    public String search() {
        HttpServletRequest request = ServletActionContext.getRequest();
//        if (request.getMethod().equals("POST")) {
//            currentUser = userService.getUser(id);
//            return "view";
//        }
        return "search";
    }

    private void addUser() {
        CedUsers user = new CedUsers();
        user.setCedUserName("zyp");
        user.setCedUserPhone(UserAction.getRandomPhone(11));
        user.setCedUserPassword("123456789015");
        user.setCedUserGender("男");
//        userService.saveUser(user);
        System.out.println("ID: " + user.getCedUserId());
        currentUser = user;
    }

    private static String getRandomPhone(int size) {
        Random r = new Random();
        StringBuilder rs = new StringBuilder();
        for (int i = 0; i < size; i++) {
            rs.append(r.nextInt(10));
        }
        return rs.toString();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public CedUsers getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(CedUsers currentUser) {
        this.currentUser = currentUser;
    }
}
